<?php

App::uses('AppController', 'Controller');

/**
 * ProductPromotions Controller
 *
 * @property ProductPromotion $ProductPromotion
 */
class ProductPromotionsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->ProductPromotion->recursive = 0;
        $this->paginate = array('order' => 'ProductPromotion.promotion_id');
        $this->set('productPromotions', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $options = array('conditions' => array('AND' => array('ProductPromotion.promotion_id' => $id)));
        $this->set('productPromotions', $this->ProductPromotion->find('all', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->ProductPromotion->create();
            if ($this->ProductPromotion->save($this->request->data)) {
                $this->Session->setFlash(__('El producto ha sido agregado a la promocion'));
                $this->redirect(array('controller' => 'promotions' ,'action' => 'index'));
            } else {
                $this->Session->setFlash(__('El producto NO ha sido agregado a la promocion. Intentelo nuevamente.'));
            }
        }
        $products = $this->ProductPromotion->Product->find('list');
        $promotions = $this->ProductPromotion->Promotion->find('list');
//        $shops = $this->ProductPromotion->Shop->find('list');
        $this->set(compact('products', 'promotions'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->ProductPromotion->exists($id)) {
            throw new NotFoundException(__('Invalid product promotion'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->ProductPromotion->save($this->request->data)) {
                $this->Session->setFlash(__('The product promotion has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The product promotion could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('ProductPromotion.' . $this->ProductPromotion->primaryKey => $id));
            $this->request->data = $this->ProductPromotion->find('first', $options);
        }
        $products = $this->ProductPromotion->Product->find('list');
        $promotions = $this->ProductPromotion->Promotion->find('list');
//        $shops = $this->ProductPromotion->Shop->find('list');
        $this->set(compact('products', 'promotions'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->ProductPromotion->id = $id;
        if (!$this->ProductPromotion->exists()) {
            throw new NotFoundException(__('Invalid product promotion'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->ProductPromotion->delete()) {
            $this->Session->setFlash(__('Product promotion deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Product promotion was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
